<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <img src="./images/登山鞋.jpg" class="img1" />
    <input type="button" value="download_image" id="button" />

    <script>
      var button = document.querySelector("#button");
      button.onclick = fn;

      function downloadIamge(selector, name) {
        console.log(1);

        // 通过选择器获取img元素，
        var img = document.querySelector(selector);
        // 将图片的src属性作为URL地址
        var url = img.src;
        var a = document.createElement("a");
        var event = new MouseEvent("click");
        console.log(event);

        a.download = name || "下载图片名称";

        a.href = url;
        console.log(a);

        a.dispatchEvent(event);
      }
      function fn() {
        return downloadIamge(".img1", "登山鞋.jpg");
      }
    </script>
  </body>
</html>
